package com.elecbook.auth.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elecbook.auth.model.dto.UserPQuery;
import com.elecbook.auth.model.dto.UserProfileDeleteDTO;
import com.elecbook.auth.model.dto.UserProfileSubmitDTO;
import com.elecbook.auth.model.po.User;
import com.elecbook.auth.mapper.UserMapper;
import com.elecbook.base.dto.PageDTO;
import com.elecbook.base.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

// 加入Mybatis-plus的内容
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserMapper, User> implements UserInfoService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User getUserInfo() {
        Integer currId = SecurityUtil.getCurrentId();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>();
        wrapper.eq(User::getId, currId);
        User user = userMapper.selectOne(wrapper);
        user.setPassword(null);
        return user;
    }

    @Transactional
    @Override
    public User updataUserProfile(UserProfileSubmitDTO dto) {
        Integer currId = SecurityUtil.getCurrentId();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>();
        wrapper.eq(User::getId, currId);
        User user = userMapper.selectOne(wrapper);
        user.setPassword(null);
        // 更新信息
        user.setAvatar(dto.getAvatar());
        user.setName(dto.getUsername());
        userMapper.updateById(user);
        User userUpdated = userMapper.selectOne(wrapper);
        return userUpdated;
    }

    @Override
    public List<User> getUserInfoByIds(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return Collections.emptyList();
        }
        // 获取这一批用户的所有信息
        return userMapper.selectBatchIds(ids);
    }

    @Override
    public void deleteUser(UserProfileDeleteDTO dto) {
        userMapper.deleteById(dto.getUserId());
    }


    /**
     * 基础分页请求--管理员
     */
    @Override
    public PageDTO<User> pageQuery(UserPQuery query) {
        // 1.添加排序规则
        // 使用内置的函数转换并且 设置规则-这里为了方便扩展和使用先进行创建
        List<OrderItem> orderItems = query.toMpOrderItems();

        // 获取分页对象和排序情况
        Page<User> page = query.toMpPageDefaultSortByIdAsc(orderItems.toArray(new OrderItem[0]));
    /*
    查询
    */
        Page<User> p = userMapper.selectPageEntity(page, query);

        // 获取数据信息
        return PageDTO.of(p, User.class);
    }


    @Override
    public User saveUser(User user) {
        user.setId(null);
        userMapper.insert(user);
        return user;
    }

    @Override
    public User updateUserById(User user) {
        userMapper.updateById(user);
        return user;
    }

}
